package com.mojito.gorm.bind;

import com.mojito.gorm.session.GormSession;

import java.lang.reflect.Proxy;

/**
 * @author mojito
 */
public class GormMapperProxyFactory<T> {
    private Class<T> mapperInterface;

    public GormMapperProxyFactory(Class<T> mapperInterface) {
        this.mapperInterface = mapperInterface;
    }

    protected T newInstance(GormMapperProxy<T> mapperProxy) {
        return (T) Proxy.newProxyInstance(mapperInterface.getClassLoader(), new Class[]{mapperInterface}, mapperProxy);
    }

    public T newInstance(GormSession sqlSession) {
        final GormMapperProxy<T> mapperProxy = new GormMapperProxy<>(mapperInterface, sqlSession);
        return newInstance(mapperProxy);
    }
}
